package com.arcbbs.service.impl;

import com.arcbbs.config.WebConfig;
import com.arcbbs.constant.BaseConstant;
import com.arcbbs.entity.EmailCode;
import com.arcbbs.entity.UserInfo;
import com.arcbbs.exception.BaseException;
import com.arcbbs.mapper.EmailCodeMapper;
import com.arcbbs.mapper.UserInfoMapper;
import com.arcbbs.service.EmailCodeService;
import com.arcbbs.utils.StringTools;
import org.apache.commons.lang3.time.DateUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import javax.mail.internet.MimeMessage;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.Date;

@Service
public class EmailCodeServiceImpl implements EmailCodeService {
@Resource
private JavaMailSender javaMailSender;
@Resource
private WebConfig webConfig;
    @Resource
    private UserInfoMapper userInfoMapper;
    @Resource
    private EmailCodeMapper emailCodeMapper;
    private static final Logger logger= LoggerFactory.getLogger(EmailCodeServiceImpl.class);
@Transactional(rollbackFor = Exception.class)
    public void sendEmailCode(String email, Integer type) {
        if (type == 0) {
            UserInfo userInfo = userInfoMapper.selectbyemail(email);
            if (userInfo != null) {
                throw new BaseException("邮箱已经存在");
            }
        }
        String code = StringTools.getFinalString(BaseConstant.LENGTH_5);
sendEmailCodeDo(email,code);
        emailCodeMapper.disablecode(email);

        EmailCode emailCode=new EmailCode();
        emailCode.setEmail(email);
        emailCode.setCode(code);
        emailCode.setCreateTime(new Date());
        emailCode.setStatus(BaseConstant.ZERO);
        emailCodeMapper.insert(emailCode);

    }
    private void sendEmailCodeDo(String toEmail,String code){
        try {
            MimeMessage message=javaMailSender.createMimeMessage();
            MimeMessageHelper mimeMessageHelper=new MimeMessageHelper(message,true);
//邮件发送人
            mimeMessageHelper.setFrom(webConfig.getSendUserName());
            mimeMessageHelper.setTo(toEmail);
            mimeMessageHelper.setSubject("注册邮箱");
            mimeMessageHelper.setText("邮箱验证码:"+code);
            mimeMessageHelper.setSentDate(new Date());
            javaMailSender.send(message);
        }catch (Exception e){
logger.error("发送邮件失败",e);
throw new BaseException("邮件发送失败");
        }

    }
    public void checkcode(String email, String emailCode){
    EmailCode dbinfo=emailCodeMapper.selectByEmailandCode(email,emailCode);
//    if(dbinfo!=null){
//        if(dbinfo.getStatus() == 0 &&System.currentTimeMillis() - dbinfo.getCreateTime().getTime()<=1000*60*15 ){
//
//        }
//    }
        if(null == dbinfo){
            throw new BaseException("邮箱验证码不正确");
        }
        if(dbinfo.getStatus() != 0 &&System.currentTimeMillis() - dbinfo.getCreateTime().getTime()>1000*60*15 ){
            throw new BaseException("邮箱验证码失效");
        }
        emailCodeMapper.disablecode(email);
    }
}
